using HKB.Common;
using System;
using System.Data;
using System.Data.SqlClient;
namespace HKB.Data
{
	public class SQLDataReader : System.IDisposable
	{
		private SqlConnection _Conn;
		private SqlDataReader _DataReader;
		private DataTable _SchemaTable;
		private DataSet _SchemaDataSet;
		private SqlCommand _Command;
		public static string connectionString = Conn.CurrenConnectionStrings;
		~SQLDataReader()
		{
			this.Close();
		}
		public void Connect()
		{
			this.Close();
			this._Conn = new SqlConnection(SQLDataReader.connectionString);
			this._Conn.Open();
		}
		public void Close()
		{
			this.Dispose();
		}
		public DataSet GetSchema(string queryString, params IDbDataParameter[] parameters)
		{
			DataSet dataSet = new DataSet();
			dataSet.EnforceConstraints = false;
			SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
			SqlCommand sqlCommand = new SqlCommand(queryString, this._Conn);
			if (parameters != null)
			{
				for (int i = 0; i < parameters.Length; i++)
				{
					SqlParameter sqlParameter = (SqlParameter)parameters[i];
					SqlParameter sqlParameter2 = new SqlParameter();
					sqlParameter2.ParameterName = sqlParameter.ParameterName;
					sqlParameter2.DbType = sqlParameter.DbType;
					sqlParameter2.Value = sqlParameter.Value;
					sqlCommand.Parameters.Add(sqlParameter2);
				}
			}
			sqlDataAdapter.SelectCommand = sqlCommand;
			sqlDataAdapter.FillSchema(dataSet, SchemaType.Mapped);
			return dataSet;
		}
		public void OpenDataReader(string queryString, params IDbDataParameter[] parameters)
		{
			this.Connect();
			this._Command = new SqlCommand(queryString, this._Conn);
			if (parameters != null)
			{
				for (int i = 0; i < parameters.Length; i++)
				{
					SqlParameter sqlParameter = (SqlParameter)parameters[i];
					SqlParameter sqlParameter2 = new SqlParameter();
					sqlParameter2.ParameterName = sqlParameter.ParameterName;
					sqlParameter2.DbType = sqlParameter.DbType;
					sqlParameter2.Value = sqlParameter.Value;
					this._Command.Parameters.Add(sqlParameter2);
				}
				this._SchemaDataSet = this.GetSchema(queryString, parameters);
			}
			else
			{
				this._SchemaDataSet = this.GetSchema(queryString, null);
			}
			this._SchemaTable = this._SchemaDataSet.Tables[0];
			this._DataReader = this._Command.ExecuteReader();
		}
		public void CloseDataReader()
		{
			try
			{
				if (!this._DataReader.IsClosed)
				{
					this._Command.Cancel();
				}
			}
			catch
			{
			}
			if (this._DataReader == null)
			{
			}
		}
		public DataRow NextRow()
		{
			DataRow result;
			if (this._DataReader.Read())
			{
				DataRow dataRow = this._SchemaTable.NewRow();
				foreach (DataColumn dataColumn in this._SchemaTable.Columns)
				{
					dataRow[dataColumn.ColumnName] = this._DataReader[dataColumn.ColumnName];
				}
				result = dataRow;
			}
			else
			{
				result = null;
			}
			return result;
		}
		public DataSet Query(string queryString)
		{
			return this.RangeQuery(queryString, 0L, -1L, new IDbDataParameter[0]);
		}
		public DataSet RangeQuery(string queryString, long first, long last, params IDbDataParameter[] param)
		{
			DataSet schemaDataSet;
			try
			{
				this.OpenDataReader(queryString, param);
				if (first < 0L)
				{
					first = 0L;
				}
				for (long num = 0L; num < first; num += 1L)
				{
					if (!this._DataReader.Read())
					{
						schemaDataSet = this._SchemaDataSet;
						return schemaDataSet;
					}
				}
				if (last < 0L)
				{
					last = 9223372036854775807L;
				}
				for (long num = first; num <= last; num += 1L)
				{
					DataRow dataRow = this.NextRow();
					if (dataRow == null)
					{
						schemaDataSet = this._SchemaDataSet;
						return schemaDataSet;
					}
					this._SchemaTable.Rows.Add(dataRow);
				}
				schemaDataSet = this._SchemaDataSet;
			}
			finally
			{
				this.CloseDataReader();
			}
			return schemaDataSet;
		}
		public void Dispose()
		{
			try
			{
				if (this._Conn != null)
				{
					if (this._Conn.State != ConnectionState.Closed && this._Conn.State != ConnectionState.Broken)
					{
						this._Conn.Close();
					}
					this._Conn = null;
				}
			}
			catch
			{
			}
		}
	}
}
